/*
 *  Copyright (c) 2017. 郑州仁中和科技有限公司.保留所有权利.
 *                        http://www.rzhkj.com/
 *        郑州仁中和科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系.
 *        代码只针对特定客户使用，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担.
 *        本代码仅用于龐帝業務系统.
 *
 */

package com.rzhkj.base.tools;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * JWT token 工具类
 * Created by lixin on 2017/9/16.
 */
@Data
public class JwtToken {
    private final static Logger logger = LoggerFactory.getLogger(JwtToken.class);

    private String clientId = "c5f04615-7739-4267-877c-bedd9ab7fed3";
    private String secretKey = "66c5879517e2d19b5586a6c767289a305b0941fbc792e40a20919bb7";
    private long expireMinutes = 5L;

    public JwtToken() {
    }

    JwtToken(String clientId, String secretKey, long expireMinutes) {
        this.clientId = clientId;
        this.secretKey = secretKey;
        this.expireMinutes = expireMinutes;
    }

    public String createToken() {
        Algorithm alg = Algorithm.HMAC256(secretKey);
        Date now = new Date();
        Date expires = new Date(now.getTime() + TimeUnit.MINUTES.toMillis(expireMinutes));

        String token = JWT.create()
                .withExpiresAt(expires)
                .withClaim("client_id", clientId)
                .sign(alg);

        return token;
    }


    public static void main(String[] args) {
        try {
            String token = new JwtToken().createToken();
            logger.info("===> Token : " + token);
//
//            OkHttpClient client = new OkHttpClient();
//            Request request = new Request.Builder()
//                    .url(PONDDY_CHINESE_URL)
//                    .get()
//                    .addHeader("accept", "application/json")
//                    .addHeader("Authorization", "JWT " + token)
//                    .build();
            try {

//                Response response = client.newCall(request).execute();
//                String json = response.body().string().trim();

//                String json = new PonddyChineseRequest()
//                        .clientId("c5f04615-7739-4267-877c-bedd9ab7fed3")
//                        .secretKey("66c5879517e2d19b5586a6c767289a305b0941fbc792e40a20919bb7")
//                        .expireMinutes("5")
//                        .httpsGet("https://chinese.ponddy.com/api/v1/pondlets/");
//
//                logger.info(json);
//
//                String New_Student_Url = "https://chinese.ponddy.com/api/v1/accounts/";
//                RequestBody formBody = new FormBody.Builder()
//                        .add("first_name", "lixin")
//                        .add("last_name", "he")
//                        .add("email", "hegaoye@qq.com")
//                        .add("password", "hegaoye123456")
//                        .build();
//
//                String result = new PonddyChineseRequest()
//                        .clientId("c5f04615-7739-4267-877c-bedd9ab7fed3")
//                        .secretKey("66c5879517e2d19b5586a6c767289a305b0941fbc792e40a20919bb7")
//                        .expireMinutes("5")
//                        .httpsPost4Jwt(New_Student_Url, formBody);
//                logger.info(result);

                //发邮件
//                String Message_Url = "https://chinese.ponddy.com/api/v1/notification/";
//                formBody = new FormEncodingBuilder()
//                        .add("studentID", "1409")
//                        .add("subject", "测试接口")
//                        .add("content", "你好 hegaoye@qq.com  测试接口")
//                        .build();
//
//                result = new PonddyChineseRequest()
//                        .clientId("c5f04615-7739-4267-877c-bedd9ab7fed3")
//                        .secretKey("66c5879517e2d19b5586a6c767289a305b0941fbc792e40a20919bb7")
//                        .expireMinutes("5")
//                        .httpsPost(Message_Url, formBody);
//                logger.info(result);
//
//
//                result = "{\"id\":1409,\"email\":\"hegaoye@qq.com\",\"first_name\":\"lixin\",\"last_name\":\"he\"}";
//                logger.info(result);
//                logger.info(result.contains("hegaoye@qq.com") + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
